import requests
from model.token import Token
import json

host = "http://221.236.10.190:3652"

import threading

def save_token_thread(data, retry=0):
    try:
        # 如果data是数组
        if isinstance(data, list):
            data = [item.__dict__ for item in data]
            url = host + "/api/parktoken/data_list"
        else:
            data = data.__dict__
            url = host + "/api/parktoken/data"

        r = requests.post(url, data=json.dumps(data))

        print(r.text)
    except Exception as e:
        if retry < 3:
            save_token(data, retry + 1)
        else:
            raise e


def save_token(data):
    threading.Thread(target=save_token_thread, args=(data, 0)).start()